package com.raja.servingStation.common.util;

import com.raja.servingStation.service.TbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * Created by tianjie on 2017/3/13.
 */
@Component
@Lazy
public class IdGenerator {

    /**
     * 1.工单ID（yyyymmdd0001）
     * 2.其他类型ID
     */
//    private int type;

    @Autowired
    private TbOrderService orderService;

    /**
     * type:1工单的id，2，其他类型的
     * orderType 1 维修工单 2 首保单
     * @param type
     * @return
     */
    public synchronized String nextVal(int type,int orderType) throws Exception{
        String nextVal = "";
        if(type==1){
            String maxId = "";
            if(orderType == 1) {
                maxId = orderService.selectMaxId();
            }
            if(orderType == 2) {
                maxId = orderService.selectFirstOrderMaxId();
            }
            if(maxId==null){
                nextVal= DateUtil.getDateYYYYMMDD()+"0001";
            }else{
                if(!DateUtil.getDateYYYYMMDD().equalsIgnoreCase(maxId.substring(0,8))){
                    nextVal= DateUtil.getDateYYYYMMDD()+"0001";
                }else {
                    String ii = String.valueOf(Integer.parseInt(maxId.substring(8,maxId.length()))+1);
                    if(String.valueOf(ii).length()==1){
                        ii="000"+ii;
                    }else if(String.valueOf(ii).length()==2){
                        ii="00"+ii;
                    }
                    else if(String.valueOf(ii).length()==3){
                        ii="0"+ii;
                    }
                    nextVal= DateUtil.getDateYYYYMMDD()+ String.valueOf(ii);
                    nextVal = orderType +nextVal;
                }
            }

        }else{
            UUID uuid = UUID.randomUUID();
            nextVal = uuid.toString();
        }
        return nextVal;
    }



}
